<extend name="Public/base" />

<block name="content">
<div id="content">
	<div id="content-header">
		<div id="breadcrumb">
			<a href="{:U('Index/index')}" title="首页" class="tip-bottom"><i
				class="icon-home"></i> 首页</a> <a href="{:U('Database/index/type/export')}" class="current">{$meta_title}</a>
		</div>
	</div>
	<div class="container-fluid">
		<br>
		<a id="export" class="btn btn-info" href="">立即备份</a>
		<a id="optimize" class="btn btn-warning" href="{:U('Database/optimize')}">优化表</a>
		<a id="repair" class="btn btn-success" href="{:U('Database/repair')}">修复表</a>
		<div class="row-fluid">
			<div class="span12">
				<div class="widget-box">
					<div class="widget-content ">
						<form id="export-form" method="post" action="{:U('export')}">
							<table class="table table-bordered table-striped with-check">
								<thead>
									<tr>
										<th><input type="checkbox" id="title-table-checkbox"
											name="title-table-checkbox" /></th>
										<th>表名</th>
										<th>数据量</th>
										<th>数据大小</th>
										<th>创建时间</th>
										<th>备份状态</th>
										<th>操作</th>
									</tr>
								</thead>
								<tbody>
									<notempty name="list">
										<volist name="list" id="table">
											<tr>
												<td><input type="checkbox" name="tables[]" value="{$table.name}" /></td>
												<td>{$table.name}</td>
												<td class="taskStatus">{$table.rows}</td>
												<td class="taskStatus">{$table.data_length|format_bytes}</td>
												<td class="taskStatus">{$table.create_time}</td>
												<td class="taskStatus info">未备份</td>
												<td class="taskStatus"><a class="ajax-get no-refresh" href="{:U('optimize?tables='.$table['name'])}"><i class="icon-legal"></i> 优化表</a>　<a class="ajax-get no-refresh" href="{:U('repair?tables='.$table['name'])}"><i class="icon-pencil"></i> 修复表</a></td>
											</tr>
										</volist>
									<else/>
										<tr>
											<td colspan="7" class="taskStatus"> Oh~亲爱滴,暂时还没有内容! </td>
										</tr>
									</notempty>
								</tbody>
							</table>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
(function($){
    var $form = $("#export-form"), $export = $("#export"), tables
        $optimize = $("#optimize"), $repair = $("#repair");

    $optimize.add($repair).click(function(){
        $.post(this.href, $form.serialize(), function(data){
            if(data.status){
				tke_alert(data.info,'alert-success');
            } else {
				tke_alert(data.info,'alert-error');
            }
            setTimeout(function(){
                $('#msg_box').find('button').click();
            },1500);
        }, "json");
        return false;
    });

    $export.click(function(){
        $export.parent().children().addClass("disabled");
        $export.html("正在发送备份请求...");
        $.post(
            $form.attr("action"),
            $form.serialize(),
            function(data){
                if(data.status){
                    tables = data.tables;
                    $export.html(data.info + "开始备份，请不要关闭本页面！");
                    backup(data.tab);
                    window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                } else {
    				tke_alert(data.info,'alert-error');
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function(){
    	                $('#msg_box').find('button').click();
    	            },1500);
                }
            },
            "json"
        );
        return false;
    });

    function backup(tab, status){
        status && showmsg(tab.id, "开始备份...(0%)");
        $.get($form.attr("action"), tab, function(data){
            if(data.status){
                showmsg(tab.id, data.info);

                if(!$.isPlainObject(data.tab)){
                    $export.parent().children().removeClass("disabled");
                    $export.html("备份完成，点击重新备份");
                    window.onbeforeunload = function(){ return null }
                    return;
                }
                backup(data.tab, tab.id != data.tab.id);
            } else {

				tke_alert(data.info,'alert-error');
                //updateAlert(data.info,'alert-error');
                $export.parent().children().removeClass("disabled");
                $export.html("立即备份");
                setTimeout(function(){
	                $('#msg_box').find('button').click();
	            },1500);
            }
        }, "json");

    }

    function showmsg(id, msg){
        $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
    }
})(jQuery);
highlight_subnav("{:U('Database/index/type/export')}");
</script>
</block>
